ccR.Priest.Discipline = {
	Valid =
	function(unit, ignoreLoS)
		if ignoreLoS == nil then ignoreLoS = false end
		if ccR.Unit.Exists(unit)
		and ccR.Unit.Is.Alive(unit)
		and ccR.Unit.Is.APlayer(unit)
		and (ignoreLoS or (not ignoreLoS and ccR.Unit.Is.InLineOfSight(unit)))
		then
			return true
		else
			return false 
		end
	end,
	PWS =
	function(unit, health, ignoreCombat)
		if ignoreCombat == nil then ignoreCombat = false end
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Has.Spell(POWER_WORD_SHIELD)
		and ccR.Spell.Is.Ready(POWER_WORD_SHIELD)
		and ccR.Unit.Health(unit) <= health
		and ((not ignoreCombat and ccR.Unit.Is.InCombat(unit)) or ignoreCombat)
		and not ccR.Unit.Has.Debuff(unit, WEAKENED_SOUL)
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(POWER_WORD_SHIELD, unit)
		end	
	end,
	Penance =
	function(unit, health)
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Has.Spell(PENANCE)
		and ccR.Spell.CanAttack(PENANCE, unit)
		and ccR.Unit.Health(unit) <= health
		and (not ccR.Player.Is.Moving() or ccR.Player.Has.Glyph(GLYPH_OF_PENANCE))
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(PENANCE, unit)
		end
	end,
	Flash_Heal =
	function(unit, health)
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Has.Spell(FLASH_HEAL)
		and ccR.Spell.CanAttack(FLASH_HEAL, unit)
		and ccR.Unit.Health(unit) <= health
		and not ccR.Player.Is.Moving()
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(FLASH_HEAL, unit)
		end
	end,
	Purify =
	function(unit)
		if ccR.Priest.Discipline.Valid(unit)
		and ccR.Player.Level() >= 22
		and ccR.Player.Has.Spell(PURIFY)
		and ccR.Spell.Is.Ready(PURIFY)
		then
			for i = 1, 40 do 
			 	local name, _, _, _, debuffType, _, _, _, _, _, debuffID = UnitDebuff(unit, i) 
			 	if name == nil then	
			 		break 
			 	else 
			 		if debuffType == "Disease"
			 		or debuffType == "Magic"
			 		then
			 			ccR.Cast.Spell(PURIFY, unit)	
			 			return
			 		end
			 	end
			end
		end
	end,
	Dispel_Magic =
	function()
	end,
	Heal =
	function(unit, health)
		if ccR.Priest.Discipline.Valid(unit)
		and ccR.Player.Level() >= 34
		and	ccR.Player.Has.Spell(HEAL)
		and ccR.Spell.CanAttack(HEAL, unit)
		and ccR.Unit.Health(unit) <= health
		and not ccR.Player.Is.Moving()
		then
			ccR.Cast.Spell(HEAL, unit)
		end
	end,
	Fade =
	function()
		if ccR.Player.Level() >= 24
		and ccR.Player.Has.Spell(FADE)
		and ccR.Spell.Is.Ready(FADE)
		and not ccR.Player.Is.CastingOrChanneling()
		then
			for k,v in ipairs(ccR_Enemies_Within_44) do
				if ccR.Unit.Exists(v)
				and ccR.Unit.Is.Alive(v)
				and (ccR.Unit.TargetName(v) == ccR.Player.Name())
				then
					ccR.Cast.Spell(FADE)
				end
			end
		end
	end,
	Prayer_of_Healing =
	function(unit, health, count)
		if ccR.Priest.Discipline.Valid(unit)
		and ccR.Player.Level() >= 46
		and ccR.Spell.Is.Ready(PRAYER_OF_HEALING)
		and ccR.Unit.Health(unit) <= health
		and not ccR.Player.Is.Moving()
		then
			local vCount = 0
			for i = 1, # ccR_Friendly do
				local vUnit = ccR_Friendly[i]
				if ccR.Priest.Discipline.Valid(unit, true)		
				and ccR.Unit.Health(vUnit) <= health
				and ccR.Unit.Distance(unit, vUnit) <= 30
				then
					vCount = vCount + 1
				end
			end
			if vCount >= count then
				if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
				ccR.Cast.Spell(PRAYER_OF_HEALING, unit)	
			end
		end
	end,
	Archangel = 
	function(stacks)
		if ccR.Player.Level() >= 50
		and ccR.Spell.Is.Ready(ARCHANGEL)
		and ccR.Player.Buff.Stacks(EVANGELISM) >= stacks
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(ARCHANGEL)
		end
	end,
	Fear_Ward = 
	function(unit)
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Level() >= 54
		and ccR.Spell.Is.Ready(FEAR_WARD)
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(FEAR_WARD, unit)
		end
	end,
	Pain_Suppression =
	function(unit, health)
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Level() >= 58
		and ccR.Spell.Is.Ready(PAIN_SUPPRESSION)
		and ccR.Spell.CanAttack(PAIN_SUPPRESSION, unit)
		and ccR.Unit.Health(unit) <= health
		and ccR.Unit.Is.InCombat(unit)
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(PAIN_SUPPRESSION, unit)
		end		
	end,
	Prayer_of_Mending =
	function(unit, health)
		if ccR.Priest.Discipline.Valid(unit)
		and	ccR.Player.Level() >= 68
		and ccR.Spell.Is.Ready(PRAYER_OF_MENDING)
		and ccR.Unit.Health(unit) <= health
		and not ccR.Player.Is.Moving()
		then
			if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
			ccR.Cast.Spell(PRAYER_OF_MENDING, unit)
		end
	end,
	Power_Word_Barrier =
	function(unit, health, count)
		if ccR.Priest.Discipline.Valid(unit)
		and ccR.Player.Level() >= 70
		and ccR.Spell.Is.Ready(POWER_WORD_BARRIER)
		and ccR.Unit.Health(unit) <= health
		and not ccR.Player.Is.Moving()
		then
			local vCount = 0
			for i = 1, # ccR_Friendly do
				local vUnit = ccR_Friendly[i]
				if ccR.Priest.Discipline.Valid(unit, true)		
				and ccR.Unit.Health(vUnit) <= health
				and ccR.Unit.Distance(unit, vUnit) <= 12
				then
					vCount = vCount + 1
				end
			end
			if vCount >= count then
				if ccR.Player.Is.CastingSpell(SMITE) then SpellStopCasting() end
				ccR.Cast.Spell(POWER_WORD_BARRIER)
				if SpellIsTargeting() then CastAtPosition(ObjectPosition(unit)) end	
			end
		end
	end,
}
setmetatable(ccR.Priest.Discipline, ccR.Priest)

--[[
for a = 1, # ccR_Enemies_In_Combat do
	local vUnit = ccR_Enemies_In_Combat[a]
	if ccR.Unit.Exists(vUnit) 
	and ccR.Unit.Is.Attackable(vUnit)
	then
		ClusterPreCount[vUnit] = 0
		for b = 1, # ccR_Enemies_In_Combat do
			local vNearbyEnemy = ccR_Enemies_In_Combat[b]
			if ccR.Unit.Exists(vNearbyEnemy) 
			and ccR.Unit.Is.Attackable(vNearbyEnemy)
			and ccR.Unit.Distance(vNearbyEnemy, vUnit) <= 12 
			then
				ClusterPreCount[vUnit] = ClusterPreCount[vUnit] + 1
			end 
		end
	end
end

local vHighestCount = 0
local vHighestUnit = nil
for c = 1, # ccR_Enemies_In_Combat do
	local vUnit = ccR_Enemies_In_Combat[c]
	if ccR.Unit.Exists(vUnit) 
	and ccR.Unit.Is.Attackable(vUnit)
	then
		if ClusterPreCount[vUnit] > vHighestCount then
			vHighestCount = ClusterPreCount[vUnit]
			vHighestUnit = vUnit
		end
	end
end

AverageUnits = {}
for d = 1, # ccR_Enemies_In_Combat do
	local vUnit = ccR_Enemies_In_Combat[d]
	if ccR.Unit.Exists(vUnit) 
	and ccR.Unit.Is.Attackable(vUnit)
	and vUnit == vHighestUnit
	then
		for e = 1, # ccR_Enemies_In_Combat do
			local vNearbyEnemy = ccR_Enemies_In_Combat[e]
			if ccR.Unit.Exists(vNearbyEnemy) 
			and ccR.Unit.Is.Attackable(vNearbyEnemy)
			and ccR.Unit.Distance(vNearbyEnemy, vUnit) <= 12 
			then
				AverageUnits[#AverageUnits+1] = vNearbyEnemy
			end 
		end
	end
end

if ccR.Unit.Exists(vHighestUnit)
and vHighestCount >= 2
and ccR.Spell.Is.Ready("Flamestrike")
then
	ccR.Cast.Spell("Flamestrike")
	if SpellIsTargeting() then
		CastAtPosition(GetAveragePositionOfObjects(AverageUnits))
		ccR.PrintToChat("Flamestrike on: " .. #AverageUnits) 
	end
end
]]